#include <iostream>
#include <memory>
#include <string>

// 抽象产品：Button
class Button {
public:
    virtual void paint() = 0;
    virtual ~Button() {}
};

// 抽象产品：TextBox
class TextBox {
public:
    virtual void render() = 0;
    virtual ~TextBox() {}
};

// Windows特定实现：WindowsButton
class WindowsButton : public Button {
public:
    void paint() override {
        std::cout << "Rendering a button in Windows style\n";
    }
};

// Windows特定实现：WindowsTextBox
class WindowsTextBox : public TextBox {
public:
    void render() override {
        std::cout << "Rendering a text box in Windows style\n";
    }
};

// macOS特定实现：MacOSButton
class MacOSButton : public Button {
public:
    void paint() override {
        std::cout << "Rendering a button in MacOS style\n";
    }
};

// macOS特定实现：MacOSTextBox
class MacOSTextBox : public TextBox {
public:
    void render() override {
        std::cout << "Rendering a text box in MacOS style\n";
    }
};

// 抽象工厂接口：GUIFactory
class GUIFactory {
public:
    virtual Button* createButton() = 0;
    virtual TextBox* createTextBox() = 0;
    virtual ~GUIFactory() {}
};

// Windows具体工厂：WindowsFactory
class WindowsFactory : public GUIFactory {
public:
    Button* createButton() override {
        return new WindowsButton();
    }
    TextBox* createTextBox() override {
        return new WindowsTextBox();
    }
};

// macOS具体工厂：MacOSFactory
class MacOSFactory : public GUIFactory {
public:
    Button* createButton() override {
        return new MacOSButton();
    }
    TextBox* createTextBox() override {
        return new MacOSTextBox();
    }
};

// 模拟检测当前运行的操作系统
enum class OS {
    WINDOWS,
    MACOS
};

// 函数用于获取当前操作系统（在实际应用中，这里应有具体的检测逻辑）
OS getCurrentOS() {
    // 这里为了演示，假设当前运行在Windows上
    // 可以更改为 OS::MACOS 来测试macOS的实现
    return OS::WINDOWS;
}

int main() {
    GUIFactory* factory = nullptr;

    // 根据当前操作系统选择相应的工厂
    OS currentOS = getCurrentOS();
    if (currentOS == OS::WINDOWS) {
        factory = new WindowsFactory();
        std::cout << "Using WindowsFactory\n";
    }
    else if (currentOS == OS::MACOS) {
        factory = new MacOSFactory();
        std::cout << "Using MacOSFactory\n";
    }
    else {
        std::cerr << "Unsupported OS!\n";
        return 1;
    }

    // 使用工厂创建UI组件
    Button* button = factory->createButton();
    TextBox* textBox = factory->createTextBox();

    // 调用组件的方法
    button->paint();
    textBox->render();

    // 释放资源
    delete button;
    delete textBox;
    delete factory;

    return 0;
}
